﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using wyxuan.yoyu.Core.IM.Games.Gobang.Dto;

namespace wyxuan.yoyu.Core.IM.Games.Gobang
{
    public class GobangService : IGobangService
    {
        private static ConcurrentBag<ChessRoom> rooms;


        public GobangService()
        {
            // 默认给一个房间
            rooms = new ConcurrentBag<ChessRoom>();
            rooms.Add(new ChessRoom
            {
                RoomId = "e7ccd938-c639-4eee-8294-470ad874b347",
                RoomName = "五子棋测试对战房间",
                ChessesList = new ConcurrentBag<ChessMan>(),
                MaxPlayPeople = 2,
                Users = new ConcurrentBag<ChessUser>()
            });
        }

        /// <summary>
        /// 加入房间
        /// </summary>
        /// <param name="roomId"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public Tuple<bool, string> JoinRoom(string roomId, string userId, string userName)
        {
            var room = rooms.FirstOrDefault(x => x.RoomId == roomId);

            if (room == null)
            {
                return new Tuple<bool, string>(false, "加入房间失败，该房间不存在或者已解散~~");
            }

            if (room.Users.Count() < room.MaxPlayPeople)
            {
                //第一个为白棋
                int chessType = room.Users.Count() == 0 ? 1 : 2;

                room.Users.Add(new ChessUser { Id = userId, UserName = userName, Action = 1, ChessType = chessType });
            }
            else
            {
                room.Users.Add(new ChessUser { Id = userId, UserName = userName, Action = 2 });
            }

            return new Tuple<bool, string>(true, "");
        }

        /// <summary>
        /// play game
        /// </summary>
        /// <param name="roomId"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public Tuple<bool, string> Play(string roomId, string userId, ChessMan chess, out string failUserid)
        {
            failUserid = string.Empty;

            var room = rooms.FirstOrDefault(x => x.RoomId == roomId);

            chess.PlayUserId = userId;

            room.ChessesList.Add(chess);

            var isWin = this.IsWin(userId);
            if (isWin)
            {
                //找出失败的id
                failUserid = room.Users.FirstOrDefault(x => x.Action == 1 && x.Id != userId)?.Id;
            }

            return new Tuple<bool, string>(isWin, isWin ? "胜利" : "继续");
        }

        /// <summary>
        /// 验证是否胜利
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        private bool IsWin(string userId)
        {
            return false;
        }

        /// <summary>
        /// 获取当前游戏中的Id
        /// </summary>
        /// <param name="roomId"></param>
        /// <returns></returns>
        public string[] GetCurrentPlayIds(string roomId)
        {
            var room = rooms.FirstOrDefault(x => x.RoomId == roomId);

            return room.Users.Where(x => x.Action == 1).Select(x => x.Id).ToArray();
        }

    }
}
